---

- name: Prepare dnf source with Consul package
  ansible.builtin.shell:
    cmd: |
          dnf install -y dnf-plugins-core
          dnf config-manager --add-repo https://rpm.releases.hashicorp.com/fedora/hashicorp.repo
    chdir: /root/
    
- name: Install Consul
  ansible.builtin.dnf:
    name: consul
    state: latest

- name: Generate Consul configuration
  ansible.builtin.template:
    src: consul-cluster.hcl.j2
    dest: /etc/consul.d/consul.hcl
    
- name: Generate host file
  ansible.builtin.template:
    src: hosts.j2
    dest: /etc/hosts

- name: Copy ACL activator
  ansible.builtin.copy:
    src: acl.hcl
    dest: /etc/consul.d/acl.hcl
  when: consul_acl_enabled == true

- name: Enable and restart Consul service
  ansible.builtin.service:
    name: consul
    daemon_reload: true
    enabled: yes
    state: restarted
